/*
 * This file is part of b.AMP.
 *
 * b.AMP is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * b.AMP is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with b.AMP.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef __BAMP_FORMAT_H__
#define __BAMP_FORMAT_H__

#include <limits.h>


/**
 * Audio encoding type. This represents the format of an individual
 * sample.
 */
typedef enum
{
    BAMP_ENC_ANY    = 0xFFFF,
    BAMP_ENC_NONE   = 0x0000,

    // Bit precision
    BAMP_ENC_8      = 0x0008,
    BAMP_ENC_16     = 0x0010,
    BAMP_ENC_24     = 0x0018,
    BAMP_ENC_32     = 0x0020,
    BAMP_ENC_64     = 0x0040,

    // Type: unsigned, signed or float
    BAMP_ENC_U      = 0x0100,
    BAMP_ENC_S      = 0x0200,
    BAMP_ENC_F      = 0x0400,

    // Encoding type: linear, u-law or A-law
    BAMP_ENC_LINEAR = 0x1000,
    BAMP_ENC_ULAW   = 0x2000,
    BAMP_ENC_ALAW   = 0x4000,

    // Unsigned encodings
    BAMP_ENC_U8     = BAMP_ENC_LINEAR | BAMP_ENC_U | BAMP_ENC_8,
    BAMP_ENC_U16    = BAMP_ENC_LINEAR | BAMP_ENC_U | BAMP_ENC_16,
    BAMP_ENC_U24    = BAMP_ENC_LINEAR | BAMP_ENC_U | BAMP_ENC_24,
    BAMP_ENC_U32    = BAMP_ENC_LINEAR | BAMP_ENC_U | BAMP_ENC_32,

    // Signed encodings
    BAMP_ENC_S8     = BAMP_ENC_LINEAR | BAMP_ENC_S | BAMP_ENC_8,
    BAMP_ENC_S16    = BAMP_ENC_LINEAR | BAMP_ENC_S | BAMP_ENC_16,
    BAMP_ENC_S24    = BAMP_ENC_LINEAR | BAMP_ENC_S | BAMP_ENC_24,
    BAMP_ENC_S32    = BAMP_ENC_LINEAR | BAMP_ENC_S | BAMP_ENC_32,

    // Floating-point encodings
    BAMP_ENC_F32    = BAMP_ENC_LINEAR | BAMP_ENC_F | BAMP_ENC_32,
    BAMP_ENC_F64    = BAMP_ENC_LINEAR | BAMP_ENC_F | BAMP_ENC_64,

    // Non-linear encodings
    BAMP_ENC_ULAW8  = BAMP_ENC_ULAW   | BAMP_ENC_U | BAMP_ENC_8,
    BAMP_ENC_ALAW8  = BAMP_ENC_ALAW   | BAMP_ENC_U | BAMP_ENC_8,
} bamp_enc;


/**
 * Byte size of an encoding type. The bit size is in the rightmost
 * byte for convenience.
 */
#define BAMP_ENC_SIZEOF(enc) ( ((enc) & 0x000000FF) >> 3 )


/**
 * Data order. Interleaved or noninterleaved. In an interleaved
 * stream, reading one sample from an n-channel stream yields n
 * samples. In a noninterleaved stream, all channels are read
 * independently.
 */
typedef enum
{
    BAMP_ORDER_ANY            = 0xFFFF,
    BAMP_ORDER_INTERLEAVED    = 0x0001,
    BAMP_ORDER_NONINTERLEAVED = 0x0002
} bamp_order;


/**
 * Constant used to read all channels at a time. This is the way to
 * read interleaved streams.
 */
#define BAMP_CHANNEL_ALL UINT_MAX

/**
 * Constant that meand "any sample rate".
 */
#define BAMP_RATE_ANY UINT_MAX


/**
 * Audio format.
 */
typedef struct
{
    bamp_enc     enc;   /// Encoding of individual samples within the stream
    unsigned int rate;  /// Sample rate
    unsigned int chan;  /// Number of channels in the stream
    bamp_order   order; /// Data order: interleaved or noninterleaved
} bamp_format;


#endif // __BAMP_FORMAT_H__
